package com.example.travel_front.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.travel_front.Model.Attraction;
import com.example.travel_front.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AttractionAdapter extends RecyclerView.Adapter<AttractionAdapter.MyViewHolder> {

    private List<Attraction> list;
    private Context mContext;
    private AttractionAdapter.OnItemClickListener onItemClickListener;
    private Map<Integer, Attraction> checkedMap = new HashMap<>();

    public AttractionAdapter(List<Attraction> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public AttractionAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View itemView = LayoutInflater.from(mContext)
                .inflate(R.layout.item_attraction, parent, false);
        return new AttractionAdapter.MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull AttractionAdapter.MyViewHolder holder, int position) {
        Attraction attraction = list.get(position);
        holder.attractionText.setText(attraction.name);
        if (checkedMap.containsValue(attraction)) {
            updateItemState(holder, true);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void loadData(List<Attraction> list, List<Attraction> checkedList) {
        this.list.clear();
        checkedMap.clear();
        this.list.addAll(list);
        checkedList.forEach(attraction -> {
            int index = list.indexOf(attraction);
            if (index != -1) {
                checkedMap.put(index, attraction);
            }
        });
        notifyDataSetChanged();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        LinearLayout attractionBackground;
        ImageView attractionStateView;
        TextView attractionText;

        public MyViewHolder(View itemView) {
            super(itemView);

            attractionBackground = itemView.findViewById(R.id.attractionBackground);
            attractionStateView = itemView.findViewById(R.id.attractionStateView);
            attractionText = itemView.findViewById(R.id.attractionText);

            attractionBackground.setOnClickListener((v) -> {
                int adapterPosition = getAdapterPosition();
                boolean checked = checkedMap.containsKey(adapterPosition);
                // 已选中
                if (checked) {
                    checkedMap.remove(adapterPosition);
                } else {
                    checkedMap.put(adapterPosition, list.get(adapterPosition));
                }
                updateItemState(this, !checked);
                onItemClickListener.onItemClick(list.get(adapterPosition), adapterPosition);
            });
        }
    }

    private void updateItemState(MyViewHolder holder, boolean checked) {
        holder.attractionBackground.setBackgroundColor(
                mContext.getResources().getColor(
                        checked ? R.color.primaryColor : R.color.black
                        , null
                )
        );
        holder.attractionStateView.setImageResource(
                checked ? R.drawable.ic_checked : R.drawable.ic_add
        );
    }

    public void setOnItemClickListener(AttractionAdapter.OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(Attraction attraction, int position);
    }
}